﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JMcms.Common;

namespace JMcms.Web.Plugin.Job.admin
{
    public partial class job_apply_list :JMcms.Web.UI.ManagePage
    {
        protected int totalCount;
        protected int page;
        protected int pageSize;

        protected int job_id = 0;


        protected void Page_Load(object sender, EventArgs e)
        {
            this.job_id = JMRequest.GetQueryInt("job_id");

            this.pageSize = GetPageSize(10); //每页数量
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("plugin_job_apply_list", JMEnums.ActionEnum.View.ToString()); //检查权限
                TreeBind(); //绑定职位
                RptBind("id>0" + CombSqlTxt(this.job_id), "add_time desc,id desc");
            }
        }

        //#region 绑定职位===============================
        private void TreeBind()
        {
            BLL.job bll = new BLL.job();
            DataTable dt = bll.GetList("").Tables[0];

            this.ddlJobId.Items.Clear();
            this.ddlJobId.Items.Add(new ListItem("所有职位", ""));
            foreach (DataRow dr in dt.Rows)
            {
                this.ddlJobId.Items.Add(new ListItem(dr["title"].ToString(), dr["id"].ToString()));
            }
        }
        //#endregion

        #region 数据绑定=================================
        private void RptBind(string _strWhere, string _orderby)
        {
            this.page = JMRequest.GetQueryInt("page", 1);
            if (this.job_id > 0)
            {
                this.ddlJobId.SelectedValue = this.job_id.ToString();
            }
            BLL.job_apply bll = new BLL.job_apply();
            this.rptList.DataSource = bll.GetList(this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList.DataBind();
            //绑定页码
            txtPageNum.Text = this.pageSize.ToString();
            string pageUrl = Utils.CombUrlTxt("job_apply_list.aspx", "job_id={0}&page={1}", this.job_id.ToString(), "__id__");
            PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, pageUrl, 8);
        }
        #endregion

        #region 组合SQL查询语句==========================
        protected string CombSqlTxt(int _job_id)
        {
            StringBuilder strTemp = new StringBuilder();
            if (_job_id > 0)
            {
                strTemp.Append(" and job_id=" + _job_id);
            }
            return strTemp.ToString();
        }
        #endregion

        #region 返回图文每页数量=========================
        private int GetPageSize(int _default_size)
        {
            int _pagesize;
            if (int.TryParse(Utils.GetCookie("job_apply_page_size"), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    return _pagesize;
                }
            }
            return _default_size;
        }
        #endregion

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int _pagesize;
            if (int.TryParse(txtPageNum.Text.Trim(), out _pagesize))
            {
                if (_pagesize > 0)
                {
                    Utils.WriteCookie("job_apply_page_size", _pagesize.ToString(), 14400);
                }
            }
            Response.Redirect(Utils.CombUrlTxt("job_apply_list.aspx", "job_id={0}", this.job_id.ToString()));
        }

        //批量删除
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ChkAdminLevel("plugin_job_apply_list", JMEnums.ActionEnum.Delete.ToString()); //检查权限
            int sucCount = 0;
            int errorCount = 0;
            BLL.job_apply bll = new BLL.job_apply();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)rptList.Items[i].FindControl("hidId")).Value);
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("chkId");
                if (cb.Checked)
                {
                    if (bll.Delete(id))
                    {
                        sucCount++;
                    }
                    else
                    {
                        errorCount++;
                    }
                }
            }
            AddAdminLog(JMEnums.ActionEnum.Delete.ToString(), "删除招聘信息成功" + sucCount + "条，失败" + errorCount + "条"); //记录日志
            JscriptMsg("删除成功" + sucCount + "条，失败" + errorCount + "条！",
                Utils.CombUrlTxt("job_apply_list.aspx", "job_id={0}", this.job_id.ToString()));
        }

        /// <summary>
        /// 获得一个职位
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Model.job GetModel(int id)
        {
            BLL.job bll = new BLL.job();
            return bll.GetModel(id);
        }

        /// <summary>
        /// 筛选职位
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ddlJobId_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect(Utils.CombUrlTxt("job_apply_list.aspx", "job_id={0}", ddlJobId.SelectedValue));
        }
    }
}